package com.sejong.andproject2;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;

public class shakingPhoneCall extends Activity implements SensorEventListener{

	    private long lastTime; 
	    private float speed; 
	    private float lastX; 
	    private float lastY; 
	    private float lastZ; 
	   

	    private float x, y, z; 
	    private static final int SHAKE_THRESHOLD = 800; 
	   

	    private static final int DATA_X = SensorManager.DATA_X; 
	    private static final int DATA_Y = SensorManager.DATA_Y; 
	    private static final int DATA_Z = SensorManager.DATA_Z; 
	   

	    private SensorManager sensorManager; 
	    private Sensor accelerormeterSensor; 
	    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.shaking);
	    // TODO Auto-generated method stub
	    

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
        accelerormeterSensor = sensorManager 
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
   
        
	}
	 @Override 
	    public void onStart() { 
	        super.onStart(); 
	   

	        if (accelerormeterSensor != null) 
	            sensorManager.registerListener(this, accelerormeterSensor, 
	                    SensorManager.SENSOR_DELAY_GAME); 
	    } 
	   

	    @Override 
	    public void onStop() { 
	        super.onStop(); 
	   

	        if (sensorManager != null) 
	            sensorManager.unregisterListener(this); 
	    } 
	   
	    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
	    } 
	   

	     
	    public void onSensorChanged(SensorEvent event) { 
	        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
	            long currentTime = System.currentTimeMillis(); 
	            long gabOfTime = (currentTime - lastTime); 
	   

	            if (gabOfTime > 100) { 
	                lastTime = currentTime; 
	   

	                x = event.values[SensorManager.DATA_X]; 
	                y = event.values[SensorManager.DATA_Y]; 
	                z = event.values[SensorManager.DATA_Z]; 
	   

	                speed = Math.abs(x + y + z - lastX - lastY - lastZ) /

	                        gabOfTime * 10000;

	   

	                if (speed > SHAKE_THRESHOLD) { 
	                  
	                //	Uri uri = Uri.parse("tell:01054762853"); 
	                  // Intent intent = new Intent(Intent.ACTION_DIAL, uri);
	                  // startActivity(intent);
	                   
	                   startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+"01049396738")));
	                } 
	                lastX = event.values[DATA_X]; 
	                lastY = event.values[DATA_Y]; 
	                lastZ = event.values[DATA_Z]; 
	            } 
	        } 
	    }   
}
